package com.kefu.call.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.call.model.CallVoiceMail;
import com.kefu.call.service.CallVoiceMailService;
import com.kefu.call.vo.CallVoiceMailSearchVo;
import com.kefu.call.vo.CallVoiceMailUpdateVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.mybatis.Order;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.sys.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "呼入留言")
@RestController
@RequestMapping("call/voiceMail")
public class CallVoiceMailController extends AbstractBaseController {

    @Autowired
    private CallVoiceMailService callVoiceMailService;
    @Autowired
    SysUserMapper sysUserMapper;

    @PostMapping("/update")
    @ApiOperation(value = "修改留言")
    public Result<String> update(@Validated @RequestBody CallVoiceMailUpdateVo chatLeaveMsgVo) {
        logger.info("修改留言:{}", chatLeaveMsgVo);

        CallVoiceMail info = chatLeaveMsgVo.convert();
        callVoiceMailService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除留言")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除留言:{}", delete);

        callVoiceMailService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }


    @PostMapping("/list")
    @ApiOperation(value = "查询留言")
    public Result<Page<CallVoiceMail>> list(@Validated @RequestBody CallVoiceMailSearchVo search) {
        search.setOrderField(null);
        search.setOrder(Order.DESC);
        Page<CallVoiceMail> page = callVoiceMailService.search(search);
        Map<String, String> userMap = sysUserMapper.getUserNameMap(KefuContext.getKefuContext().getTenantId());
        page.getRecords().forEach(item -> item.setUpdateUserName(userMap.get(item.getUpdateUser())));
        return ResultGenerator.genSuccessResult(page);
    }
}
